<template>
  <div class="dashboard-editor-container">
    <el-row :gutter="20" class="panel-group">
      <el-col v-for="(item,index) in totalData" :key="index" :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <card-panel
          :url="item.url"
          :type="item.type"
          :color="item.color"
          :icon-class="item.iconClass"
          :card-panel-text="item.title"
          :is-danger="item.isDanger"
          :label-text="item.labelText"
          :total="item.total"
          :duration="item.duration"
          :is-chart="item.isChart"
          @handleSetLineChartData="handleSetLineChartData"
        />
      </el-col>
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div class="widget-title">近七日{{ title }}走势</div>
      <line-chart :chart-data="lineChartData" />
    </el-row>
    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          <div class="widget-title">文章数类别统计</div>
          <pie-chart :chart-data="pieChartDataTotal"/>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="12">
        <div class="chart-wrapper">
          <div class="widget-title">今日爬虫访问统计</div>
          <pie-chart :chart-data="pieChartDataSpider"/>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getLineChartData, getLineChartTotal, getPieChartDataTotal, getPieChartDataSpider } from '@/api/system'
import CardPanel from './components/CardPanel'
import LineChart from './components/LineChart'
import PieChart from './components/PieChart'

export default {
  name: 'DashboardAdmin',
  components: {
    CardPanel,
    LineChart,
    PieChart
  },
  data() {
    return {
      lineChartData: {},
      listLoading: true,
      totalData: {},
      title: '',
      pieChartDataTotal: {},
      pieChartDataSpider: {},
      timer: ''
    }
  },
  created() {
    // 折线
    this.getLineChartTotal()
    // 分类文章数
    this.getPieChartDataTotal()
    // 今日爬虫统计
    this.getPieChartDataSpider()
    // 折线默认近一周访问统计
    this.handleSetLineChartData('')
  },
  mounted: function() {
    this.timer = setInterval(this.refreshEcharts, 1000 * 60)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    handleSetLineChartData(type) {
      this.listLoading = true
      getLineChartData(type).then(response => {
        this.lineChartData = response.data
        this.title = response.data.expectedData.title
        this.listLoading = false
      })
    },
    getLineChartTotal() {
      this.listLoading = true
      getLineChartTotal().then(response => {
        this.totalData = response.data
        this.listLoading = false
      })
    },
    getPieChartDataTotal() {
      getPieChartDataTotal().then(response => {
        this.pieChartDataTotal = response.data
      })
    },
    getPieChartDataSpider() {
      getPieChartDataSpider().then(response => {
        this.pieChartDataSpider = response.data
      })
    },
    refreshEcharts() {
      // 折线
      this.getLineChartTotal()
      // 分类文章数
      this.getPieChartDataTotal()
      // 今日爬虫统计
      this.getPieChartDataSpider()
      // 折线默认近一周访问统计
      this.handleSetLineChartData('')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
  .panel-group {
    margin-top: 18px;

    .card-panel-col {
      margin-bottom: 32px;
    }
  }
}
</style>
